﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DataObjectsPhone;
using System.Linq;
using System.Xml;
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
using System.Diagnostics;
using System.Collections.ObjectModel;

namespace ee_wp_tourmaker.DataControllers
{
    public class ProfileData
    {
        public Guid Id { get { return _player.Id; } }
        public string Name { get { return _player.Name; } }

        Player _player;

        public ObservableCollection<Player> playerList = new ObservableCollection<Player>();

        private static String PLAYER_FILE = "playerfile.xml";

        public void LoadProfileData()
        {
            try
            {
                using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (isoStorage.FileExists(PLAYER_FILE))
                    {
                        using (var file = isoStorage.OpenFile(PLAYER_FILE, FileMode.OpenOrCreate))
                        {
                            using (var reader = new StreamReader(file))
                            {
                                if (file.Length > 0)
                                {
                                    XmlSerializer xs = new XmlSerializer(typeof(Player));
                                    _player= xs.Deserialize(reader) as Player;
                                }
                            }
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("failed to load player! {0}", ex));
            }
        }

        public void SaveProfileData()
        {
            try
            {
                using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (var file = isoStorage.OpenFile(PLAYER_FILE, FileMode.Create, FileAccess.Write))
                    {
                        using (var writer = new StreamWriter(file))
                        {
                            XmlSerializer xs = new XmlSerializer(typeof(Player));
                            xs.Serialize(writer, _player);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("failed to save player! {0}", ex));
            }
        }

        public bool IsLoaded
        {
            get
            {
                return _player != null;
            }
        }
        internal void ChangeName(string newName)
        {
            _player.Name = newName;
        }

        internal void Create()
        {
            _player = new Player
            {
                Id = Guid.NewGuid(),
                Name = Microsoft.Phone.Info.DeviceStatus.DeviceName
            };
        }
    }
}
